﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Templay
{
    public class ParseElementCollection : List<ParseElement>
    {

        private Dictionary<string, ParseElement> Styles { get; set; }

        private RootElement root;

        public RootElement Root
        {
            get
            {
                if (root == null)
                    root = this.Single(x => x.Type == ParseElementType.ROOT).ToRootElement();

                return root;
            }
        }

        private StyleTemplateStructure defaultStructure;

        public ParseElementCollection()
        {
            Styles = new Dictionary<string, ParseElement>();
            defaultStructure = StyleTemplateStructure.Default;
        }

        public void GetStyle(IEnumerable<ParseElement> elements)
        {
            if (Styles.Count > 0)
                return;

            var styleItems = elements.Single(x => x.Name.ToLower() == "root").Elements.
                Where(x => x.Name == defaultStructure.Header);

            foreach (ParseElement styleItem in styleItems)
                GetItem(styleItem);
        }

        private void GetItem(ParseElement styleItem, ParseElement parent = null)
        {
            var itemItems = styleItem.Elements.Where(x => x.Name == defaultStructure.Item);

            foreach (ParseElement itemItem in itemItems)
            {
                var attributeItems = itemItem.Elements.Where(x => x.Name == defaultStructure.Attribute);
                var childrenItems = itemItem.Elements.Where(x => x.Name == defaultStructure.Item);

                ParseElement styleElement;

                if (parent != null)
                    styleElement = parent.Clone() as ParseElement;
                else
                    styleElement = new ParseElement();

                styleElement.Name = itemItem.Attributes[defaultStructure.ItemName].Value;
                styleElement.Type = ParseElementType.STYLE;

                //Set attributes
                foreach (ParseElement attributeItem in attributeItems)
                {
                    styleElement.Attributes.Add(attributeItem[defaultStructure.AttributeKey],
                        new ParseAttribute() { Name = attributeItem[defaultStructure.AttributeKey], Value = attributeItem[defaultStructure.AttributeValue] });
                }

                Styles.Add(styleElement.Name, styleElement);

                GetItem(itemItem, styleElement);

            }

        }

        public void ApplyStyle()
        {
            GetStyle(this);
            ApplyStyle(this);
        }

        public void ApplyStyle(IEnumerable<ParseElement> elements)
        {
            foreach (ParseElement element in elements)
            {
                if (element.Attributes.ContainsKey(defaultStructure.Header))
                {
                    //Get style class
                    string styles = element[defaultStructure.Header];
                    styles = styles.Replace(" ", "");
                    string[] allClasses = styles.Split(',');

                    foreach (string styleclass in allClasses)
                    {
                        //Apply
                        if (Styles.ContainsKey(styleclass))
                            element.ApplyStyle(Styles[styleclass]);
                    }

                }

                //Childrens
                ApplyStyle(element.Elements);
            }
        }


    }
}
